package org.keeners.imageviewer {
	
	import flash.display.*;
	
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	
	import flash.events.KeyboardEvent;   
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import fl.events.SliderEvent;
	
	import flash.geom.ColorTransform
	import fl.controls.Slider
	import fl.controls.SliderDirection;

	public class ImageViewerControls extends Sprite {
		
		public var imageviewer_base:ImageViewerBase;
			 
		public var hud_drag:Sprite;
		public var hud_zoom:Sprite;
		public var hud_reset:Sprite;
		public var hud_current:String;
		
		public var zoom_slider:Sprite;
		public var zoom_slider_component:Slider;
		
		public var control_properties:Object;
		//public var button_properties:Object;
		public var buttons:Array;
				
		public function ImageViewerControls(base:ImageViewerBase):void { 
			/*
				creates control set including two hud elements and the
				zoom slider
			*/
			
			imageviewer_base = base;
			
			control_properties = {};
			control_properties['default'] = {color:0xdddddd, alpha:.7}
			control_properties['active'] = {color:0xffff00, alpha:1}
			
			//button_properties = {};
			//button_properties['drag'] = {is_button: false}
			//button_properties['zoom'] = {is_button: false}
						
			buttons = [];
			
			var hud_padding:Number = 5;
			hud_drag = create_button('drag', new mc_controls_button(), new mc_icon_grabber());
			hud_zoom = create_button('zoom', new mc_controls_button(), new mc_icon_zoom_in());	
			
			hud_zoom.x = hud_padding + hud_drag.x + hud_drag.width;
			
			zoom_slider = new Sprite();
			var cz:MovieClip = new mc_controls_zoom()
			zoom_slider.addChild(cz);
			zoom_slider.x = hud_zoom.x + 4;
			zoom_slider.y = hud_zoom.y + hud_zoom.height + 5;
			
			var zib = cz.getChildByName('zoom_in_button');
			var zob = cz.getChildByName('zoom_out_button');
			var zbuttons:Array = [[zib,1], [zob,-1]];
			for (var i=0;i<zbuttons.length;i++){
				var zb = zbuttons[i];
				var zb_clip = zb[0];
				zb_clip.buttonMode = true;
				zb_clip.useHandCursor = true;
			}
			zib.addEventListener(MouseEvent.CLICK, function():void{ imageviewer_base.image_zoom(imageviewer_base.image_zoom_current + 1, []); });			
			zob.addEventListener(MouseEvent.CLICK, function():void{ imageviewer_base.image_zoom(imageviewer_base.image_zoom_current - 1, []); });
			
			zoom_slider_component = new Slider();			
			zoom_slider_component.direction = SliderDirection.VERTICAL;
			
			zoom_slider_component.y = 26;
			zoom_slider_component.x = 12;
			zoom_slider_component.width = 30;
			zoom_slider_component.height = 40;
			zoom_slider_component.alpha = 2;
			
			zoom_slider_component.liveDragging = true;
			zoom_slider.addChild(zoom_slider_component);
			
			zoom_slider_component.addEventListener(SliderEvent.CHANGE,zoom_slider_zoom);
						
			addChild(hud_drag);
			addChild(hud_zoom);
			addChild(zoom_slider);
			
			
			activate_drag();			
			
		}
		
		public function zoom_slider_set_size(levels:int):void {
			/*
				simple size updater for the zoom slider, called
				after the image is loaded as the number of levels is 
				determined by the image dimensions
			*/
			
			zoom_slider_component.minimum = 0;
			zoom_slider_component.maximum = levels - 1;
			zoom_slider_component.tickInterval = 1;

		}
		public function zoom_slider_zoom(e:SliderEvent):void {
			/*
				slider handlers, zooms in/out given a +/- delta
			*/
						
			imageviewer_base.image_zoom(e.target.value,[])
		}
		public function activate_zoom_in():void { 
			/*
				activates zoom, updates hud and hud_current which in turn
				affects the outcome of a mouse click
			*/
			
			button_activate(hud_zoom);
			button_swap_icon(hud_zoom, new mc_icon_zoom_in());
			hud_current = 'zoom_in';
		}
		
		public function activate_zoom_out():void { 
			/*
				activates zoom out, updates hud and hud_current which in turn
				affects the outcome of a mouse click
			*/
			button_activate(hud_zoom);
			button_swap_icon(hud_zoom, new mc_icon_zoom_out());
			hud_current = 'zoom_out';			
		}
		
		public function activate_drag():void { 
			/*
				activates drag, updates hud and hud_current which in turn
				affects the outcome of a mouse click
			*/
			
			button_activate(hud_drag);
			button_swap_icon(hud_zoom, new mc_icon_zoom_in());
			hud_current = 'drag';
		}
		
		public function button_activate(button:Sprite):void {		 
			/*
				handles visual change when zoom in/out or drag is
				activated
			*/
				
			for (var i:int=0; i <buttons.length;i++){
				var cbutton = buttons[i];
				var cname = cbutton.name;
				var box = cbutton.getChildByName('box');
				var bg = box.getChildByName('background');			
				var ct:ColorTransform = bg.transform.colorTransform;
				var props = cname == button.name ? 'active' : 'default';
				
				ct.color = control_properties[props]['color'];								
				bg.transform.colorTransform = ct;	   		
				bg.alpha = control_properties[props]['alpha'];				
			}
			
		}
		
		public function button_swap_icon(button:Sprite, button_icon):void {
			/*
				swaps an icon for a hud, e.g. alt key press swaps zoom out
				for zoom in
			*/
			 	
			try{
				var current_icon = button.getChildByName('icon');
				button.removeChild(current_icon);
	   	 	}catch(e){
	   	 		// does not exist	
	   	 	}
	   	 	button_icon._quality = "LOW";
			button.addChild(button_icon);
			button_icon.name = 'icon';
			button_icon.x = (button.width - button_icon.width) / 2
			button_icon.y = (button.height - button_icon.height) / 2
		}
		
		public function create_button(id, base_mc, base_icon):Sprite { 
			/*
				psuedo factory pattern for hud creation
			*/			
			
			var button = new Sprite();
			button.name = id;
						
			button.addChild(base_mc);
			base_mc.name =  'box';
						
			button_swap_icon(button, base_icon);			
			buttons.push(button);
			
			return button;
			
		}
				
	}
	
}